package com.line.service;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.springframework.stereotype.Service;

import com.config.dao.ChargeConfigDao;
import com.config.dao.EquipmentDao;
import com.config.dao.LineConfigDao;
import com.config.dao.ShiftConfigDao;
import com.config.entity.Equipment;
import com.config.entity.LineConfig;
import com.config.entity.ShiftConfig;
import com.data.dao.ChargeDayDao;
import com.data.dao.ChargeMinDao;
import com.data.dao.ChargeMonthDao;
import com.data.dao.ChargeShiftDao;
import com.data.dao.ChargeWeekDao;
import com.data.dao.StateDayDao;
import com.data.dao.StateMinDao;
import com.data.dao.StateMonthDao;
import com.data.dao.StateWeekDao;
import com.data.entity.ChargeDay;
import com.data.entity.ChargeMin;
import com.data.entity.ChargeMonth;
import com.data.entity.ChargeShift;
import com.data.entity.ChargeWeek;
import com.data.entity.StateDay;
import com.data.entity.StateMin;
import com.data.entity.StateMonth;
import com.data.entity.StateWeek;
import com.line.dao.LineDao;

@Service("lineService")
public class LineServiceImpl implements LineService{
	@Resource(name="equipmentDao")
	private  EquipmentDao equipmentDao;
	@Resource(name = "lineConfigDao")
	private LineConfigDao lineConfigDao;
	@Resource(name = "lineDao")
	private LineDao lineDao;
    /**
     * 整线小时电度电费统计
     * @param lineId
     * @param belongTime
     * @param model
     */
	@Override
	public void lineChargesByDay(int lineId, String belongTime,Map<String, Object> model) {
		DecimalFormat  df = new DecimalFormat("0.0");  
		double totalCharges=0.0;double totalRelativeComps=0.0;
		List<String> equNos=new ArrayList<String>();
		List<Double> charges=new ArrayList<Double>();
		List<Double> relativeComps=new ArrayList<Double>();
		LineConfig line=null;
		if(lineId==0){
			line=lineConfigDao.findFirstLine();
		}else{
			line=lineConfigDao.get(lineId);
		}
		if(line!=null){
			DateTimeFormatter format=DateTimeFormat.forPattern("yyyy-MM-dd HH:mm");  
			DateTime dt = DateTime.parse(belongTime,format);
			String startTime=dt.minusHours(1).toString(format);
			List<ChargeMin> list=lineDao.lineChargesByDay(startTime,belongTime);
			if(list.size()!=0){
				for(ChargeMin cm:list){
					totalCharges+=cm.getCharge();totalRelativeComps+=cm.getRelativeComp();
					charges.add(cm.getCharge());
					relativeComps.add(cm.getRelativeComp());
					Equipment e=equipmentDao.get(cm.getEquId());
					if(e!=null){
						equNos.add(e.getEquNo());
					}
				}
			}
		}
		model.put("equNos", equNos);
		model.put("charges", charges);
		model.put("line", line);
		model.put("relativeComps", relativeComps);
		model.put("totalCharges", df.format(totalCharges));
		model.put("totalRelativeComps", df.format(totalRelativeComps));
	}
	 /**
     * 整线每日电度电费统计
     * @param lineId
     * @param selectDate
     * @param model
     */
	@Override
	public void lineChargesByDate(int lineId, String startDate,String endDate,Map<String, Object> model) {
		DecimalFormat  df = new DecimalFormat("0.0");  
		double totalCharges=0.0;double totalRelativeComps=0.0;
		List<String> equNos=new ArrayList<String>();
		List<Double> charges=new ArrayList<Double>();
		List<Double> relativeComps=new ArrayList<Double>();
		LineConfig line=null;
		if(lineId==0){
			line=lineConfigDao.findFirstLine();
		}else{
			line=lineConfigDao.get(lineId);
		}
		if(line!=null){
			List<ChargeDay> list=lineDao.lineChargesByDate(startDate, endDate);
			if(list.size()!=0){
				for(ChargeDay cm:list){
					totalCharges+=cm.getCharge();totalRelativeComps+=cm.getRelativeComp();
					charges.add(cm.getCharge());
					relativeComps.add(cm.getRelativeComp());
					Equipment e=equipmentDao.get(cm.getEquId());
					if(e!=null){
						equNos.add(e.getEquNo());
					}
				}
			}
		}
		model.put("equNos", equNos);
		model.put("charges", charges);
		model.put("line", line);
		model.put("relativeComps", relativeComps);
		model.put("totalCharges", df.format(totalCharges));
		model.put("totalRelativeComps", df.format(totalRelativeComps));
	}
	/**
	 * 整线电度电费周统计
	 * @param lineId
	 * @param belongYear
	 * @param startWeek
	 * @param endWeek
	 * @param model
	 */
	@Override
	public void lineChargesByWeek(int lineId, int belongYear, int startWeek,
			int endWeek, Map<String, Object> model) {
		DecimalFormat  df = new DecimalFormat("0.0");  
		double totalCharges=0.0;double totalRelativeComps=0.0;
		List<String> equNos=new ArrayList<String>();
		List<Double> charges=new ArrayList<Double>();
		List<Double> relativeComps=new ArrayList<Double>();
		LineConfig line=null;
		if(lineId==0){
			line=lineConfigDao.findFirstLine();
		}else{
			line=lineConfigDao.get(lineId);
		}
		if(line!=null){
			List<ChargeWeek> list=lineDao.lineChargesByWeek(belongYear, startWeek, endWeek);
			if(list.size()!=0){
				for(ChargeWeek cm:list){
					totalCharges+=cm.getCharge();totalRelativeComps+=cm.getRelativeComp();
					charges.add(cm.getCharge());
					relativeComps.add(cm.getRelativeComp());
					Equipment e=equipmentDao.get(cm.getEquId());
					if(e!=null){
						equNos.add(e.getEquNo());
					}
				}
			}
		}
		model.put("equNos", equNos);
		model.put("charges", charges);
		model.put("line", line);
		model.put("relativeComps", relativeComps);
		model.put("totalCharges", df.format(totalCharges));
		model.put("totalRelativeComps", df.format(totalRelativeComps));
		
	}
	/**
	 * 整线电度电费月统计
	 * @param lineId
	 * @param startDate
	 * @param endDate
	 * @param model
	 */
	@Override
	public void lineChargesByMonth(int lineId, String startDate,
			String endDate, Map<String, Object> model) {
		DecimalFormat  df = new DecimalFormat("0.0");  
		double totalCharges=0.0;double totalRelativeComps=0.0;
		List<String> equNos=new ArrayList<String>();
		List<Double> charges=new ArrayList<Double>();
		List<Double> relativeComps=new ArrayList<Double>();
		LineConfig line=null;
		if(lineId==0){
			line=lineConfigDao.findFirstLine();
		}else{
			line=lineConfigDao.get(lineId);
		}
		if(line!=null){
			List<ChargeMonth> list=lineDao.lineChargesByMonth(startDate, endDate);
			if(list.size()!=0){
				for(ChargeMonth cm:list){
					totalCharges+=cm.getCharge();totalRelativeComps+=cm.getRelativeComp();
					charges.add(cm.getCharge());
					relativeComps.add(cm.getRelativeComp());
					Equipment e=equipmentDao.get(cm.getEquId());
					if(e!=null){
						equNos.add(e.getEquNo());
					}
				}
			}
		}
		model.put("equNos", equNos);
		model.put("charges", charges);
		model.put("line", line);
		model.put("relativeComps", relativeComps);
		model.put("totalCharges", df.format(totalCharges));
		model.put("totalRelativeComps", df.format(totalRelativeComps));
	}
}
